<?php
 
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;

class ReferenceController extends ControllerBase
{

    /**
     * Index action
     */
    public function indexAction()
    {
        $this->persistent->parameters = null;
    }

    /**
     * Searches for reference
     */
    public function searchAction()
    {

        $numberPage = 1;
        if ($this->request->isPost()) {
            $query = Criteria::fromInput($this->di, "Reference", $_POST);
            $this->persistent->parameters = $query->getParams();
        } else {
            $numberPage = $this->request->getQuery("page", "int");
        }

        $parameters = $this->persistent->parameters;
        if (!is_array($parameters)) {
            $parameters = array();
        }
        $parameters["order"] = "id";

        $reference = Reference::find($parameters);
        if (count($reference) == 0) {
            $this->flash->notice("The search did not find any reference");

            return $this->dispatcher->forward(array(
                "controller" => "reference",
                "action" => "index"
            ));
        }

        $paginator = new Paginator(array(
            "data" => $reference,
            "limit"=> 10,
            "page" => $numberPage
        ));

        $this->view->page = $paginator->getPaginate();
    }

    /**
     * Displayes the creation form
     */
    public function newAction()
    {

    }

    /**
     * Edits a reference
     *
     * @param string $id
     */
    public function editAction($id)
    {

        if (!$this->request->isPost()) {

            $reference = Reference::findFirstByid($id);
            if (!$reference) {
                $this->flash->error("reference was not found");

                return $this->dispatcher->forward(array(
                    "controller" => "reference",
                    "action" => "index"
                ));
            }

            $this->view->id = $reference->id;

            $this->tag->setDefault("id", $reference->id);
            $this->tag->setDefault("content", $reference->content);
            $this->tag->setDefault("questions", $reference->questions);
            $this->tag->setDefault("level", $reference->level);
            $this->tag->setDefault("type", $reference->type);
            
        }
    }

    /**
     * Creates a new reference
     */
    public function createAction()
    {

        if (!$this->request->isPost()) {
            return $this->dispatcher->forward(array(
                "controller" => "reference",
                "action" => "index"
            ));
        }

        $reference = new Reference();

        $reference->id = $this->request->getPost("id");
        $reference->content = $this->request->getPost("content");
        $reference->questions = $this->request->getPost("questions");
        $reference->level = $this->request->getPost("level");
        $reference->type = $this->request->getPost("type");
        

        if (!$reference->save()) {
            foreach ($reference->getMessages() as $message) {
                $this->flash->error($message);
            }

            return $this->dispatcher->forward(array(
                "controller" => "reference",
                "action" => "new"
            ));
        }

        $this->flash->success("reference was created successfully");

        return $this->dispatcher->forward(array(
            "controller" => "reference",
            "action" => "index"
        ));

    }

    /**
     * Saves a reference edited
     *
     */
    public function saveAction()
    {

        if (!$this->request->isPost()) {
            return $this->dispatcher->forward(array(
                "controller" => "reference",
                "action" => "index"
            ));
        }

        $id = $this->request->getPost("id");

        $reference = Reference::findFirstByid($id);
        if (!$reference) {
            $this->flash->error("reference does not exist " . $id);

            return $this->dispatcher->forward(array(
                "controller" => "reference",
                "action" => "index"
            ));
        }

        $reference->id = $this->request->getPost("id");
        $reference->content = $this->request->getPost("content");
        $reference->questions = $this->request->getPost("questions");
        $reference->level = $this->request->getPost("level");
        $reference->type = $this->request->getPost("type");
        

        if (!$reference->save()) {

            foreach ($reference->getMessages() as $message) {
                $this->flash->error($message);
            }

            return $this->dispatcher->forward(array(
                "controller" => "reference",
                "action" => "edit",
                "params" => array($reference->id)
            ));
        }

        $this->flash->success("reference was updated successfully");

        return $this->dispatcher->forward(array(
            "controller" => "reference",
            "action" => "index"
        ));

    }

    /**
     * Deletes a reference
     *
     * @param string $id
     */
    public function deleteAction($id)
    {

        $reference = Reference::findFirstByid($id);
        if (!$reference) {
            $this->flash->error("reference was not found");

            return $this->dispatcher->forward(array(
                "controller" => "reference",
                "action" => "index"
            ));
        }

        if (!$reference->delete()) {

            foreach ($reference->getMessages() as $message) {
                $this->flash->error($message);
            }

            return $this->dispatcher->forward(array(
                "controller" => "reference",
                "action" => "search"
            ));
        }

        $this->flash->success("reference was deleted successfully");

        return $this->dispatcher->forward(array(
            "controller" => "reference",
            "action" => "index"
        ));
    }

}
